<template>
  <div>
    <div><h1>类型列表</h1></div>
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="请选择关键字/spu/商品名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="searchForm.typeId"
            placeholder="请选择类型状态"
            clearable
            style="width: 140px"
          >
            <el-option label="隐藏" :value="0" />
            <el-option label="开启" :value="1" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="loadTypeList()">查询</el-button>
          <el-button type="primary" @click="onaddType()">添加类型</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table
        :data="typeList"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        border
      >
        <el-table-column prop="id" label="Id" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="parentId" label="父Id" />
        <el-table-column prop="typeTop" label="排序" />
        <el-table-column prop="typeImages" label="图片">
          <template #default="scope">
            <el-image
              v-if="scope.row.typeImages != null"
              :src="scope.row.typeImages"
              style="width: 70px; height: 70px"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="typeStatus" label="状态">
          <template #default="scope">
            <span style="color: red" v-if="scope.row.typeStatus == 0"
              >隐藏</span
            >
            <span style="color: green" v-if="scope.row.typeStatus == 1"
              >开启</span
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" />
      </el-table>
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加类型" width="500">
        <el-form ref="ruleFormRef" :model="form" :rules="rules">
          <el-form-item
            label="类型名称"
            :label-width="formLabelWidth"
            prop="typeName"
          >
            <el-input v-model="form.typeName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="图片"
            :label-width="formLabelWidth"
            prop="typeImage"
          >
            <el-upload
              class="avatar-uploader"
              action="http://localhost:10010/prod/type/uploadFile"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :headers="headers"
            >
              <img v-if="form.typeImage" :src="form.typeImage" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item
            label="排序"
            prop="typeTop"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.typeTop" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="状态"
            prop="typeStatus"
            :label-width="formLabelWidth"
          >
            <el-radio-group v-model="form.typeStatus">
              <el-radio :value="0" size="large">隐藏</el-radio>
              <el-radio :value="1" size="large">开启</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">关闭</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              添加
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getTypeTree, addType } from "../api/api";
import { Plus } from "@element-plus/icons-vue";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import type { UploadProps } from "element-plus";
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const ruleFormRef = ref<FormInstance>();

const form = ref({
  typeName: "",
  typeStatus: "",
  typeTop: "",
  typeImage: "",
});
const searchForm = ref({
  keyWord: null,
  typeId: null,
});
const typeList = ref([]);
const loadTypeList = () => {
  getTypeTree(searchForm.value).then((res) => {
    typeList.value = res.data.data;
  });
};
onMounted(() => {
  loadTypeList();
});
//上传图片
const headers = ref({
  token: sessionStorage.getItem("token"),
});
const handleAvatarSuccess: UploadProps["onSuccess"] = (res, uploadFile) => {
  form.value.typeImage = res.data;
};
//校验
const rules = ref<FormRules>({
  typeName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
  typeImage: [{ required: true, message: "图片不能为空", trigger: "blur" }],
  typeTop: [{ required: true, message: "排序不能为空", trigger: "blur" }],
  typeStatus: [{ required: true, message: "状态不能为空", trigger: "blur" }],
});

//添加
const onaddType = () => {
  form.value = {
    typeName: "",
    typeStatus: "",
    typeTop: "",
    typeImage: "",
  };
  dialogFormVisible.value = true;
};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      addType(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          loadTypeList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
